<script src="{{ getAssets }}js/amazeui.pagination.js?v={{ assets_version }}"></script>
<script>
var curr_page = {'lens' : 0};
var goods_type = 'lens';
var get_goods_url = '{{ urlfor "RetailController.History" }}';
var query_data = {};
$("input").on("keypress",function(e){
    if (e.which == 13) {
        query(goods_type);
    }
});
function query(goods_type){
    var data=$("form[data-type='"+goods_type+"']").serializeObject();
    var reserve_num=[];
    reserve_num.push("1");
    data.goods_type=goods_type;
    data.reserve_num=reserve_num;
    query_data = data;
    ajax_database_products_list(goods_type);
}
function ajax_database_products_list(goods_type) {
    query_data.offset = curr_page[goods_type] * query_data.limit;
    query_data.limit = 10;
    query_data.archives_id = Number(getQueryValue('archives_id'));
    var box = $('form[data-type="'+ goods_type +'"]').find('#'+ goods_type +'_datalist');
    $.post(get_goods_url, JSON.stringify(query_data), function (res) {
        if(curr_page[goods_type] == 0){
            get_database_pagination(goods_type, Math.ceil(res.iTotalDisplayRecords/query_data.limit));
        }
        box.html('');
        $.each(res.aaData, function (index, value) {
            box.append(set_list_rows_html(index, value));
            query_data[value.Id] = value;
        });
    }, 'json');
}
function get_database_pagination(goods_type, count) {
    new Pagination({
        wrap: $('.am-pagination-'+goods_type), // 存放分页内容的容器
        count: count, // 总页数
        current: 1, // 当前的页数（默认为1）
        prevText: '上一页', // prev 按钮的文本内容
        nextText: '下一页', // next 按钮的文本内容
        callback: function(page) { // 每一个页数按钮的回调事件
            curr_page[goods_type] = page-1;
            ajax_database_products_list(goods_type);
        }
    });
}
function set_list_rows_html(index, data) {
    var html = [];
    html.push('<div class="goods-info-row" data-index="'+ data.Id +'" data-custom="'+ data.Custom.Id +'" onclick="select_history(this);return false;">');
    html.push('<div class="item g-name"><span>'+ productGroupName(data.Products) +'</span></div>');
    html.push('<div class="item" style="padding-right:100px;">&nbsp;');
    data.Snd.sph ? html.push('&nbsp;&nbsp;'+data.Snd.sph) : '';
    data.Snd.cyl ? html.push('&nbsp;&nbsp;'+data.Snd.cyl) : '';
    data.Snd.add ? html.push('&nbsp;&nbsp;'+data.Snd.add) : '';
    data.Snd.model ? html.push('&nbsp;&nbsp;'+data.Snd.model) : '';
    data.Snd.color ? html.push('&nbsp;&nbsp;'+data.Snd.color) : '';
    html.push('</div>');
    html.push('<div class="item g-price">'+ timetransDate(data.Retails.CreatedAt) +'</div>');
    html.push('<div class="item g-price">'+ data.Amount +'</div>');
    if (data.Custom.Id == 0) {
        html.push('<div class="item g-price am-text-danger">￥'+ data.UnitPrice +'</div>');
        html.push('<div class="item g-price">￥'+ (data.Inventory.suggested_price > 0 ? data.Inventory.suggested_price : '未定价') +'</div>');
    } else {
        html.push('<div class="item g-price am-text-danger">定制商品</div>');
        html.push('<div class="item g-price">定制商品</div>');
    }
    html.push('<div class="item g-price am-text-danger">'+ data.Inventory.purchase_num +'</div>');
    html.push('</div>');
    return html.join('');
}
function select_history(that) {
    var index = $(that).data('index');
    var custom = $(that).data('custom');
    var _data = query_data[index];
    var inventory = _data.Inventory;
    if(custom != 0) {
        layer.alert("定制商品仅供参考");return false;
    }
    if(_data.Inventory.suggested_price == 0) {
        layer.alert("商品暂未定价，请先定价");return false;
    }
    inventory.Products = _data.Products;
    inventory.Snd = _data.Snd;

    inventory.data_type = "goods";
    var arryString = [];
    inventory.name = productGroupName(_data.Products);
    /************二级参数**********************/
    inventory.subparams = [];
    if (inventory.Products.TypeId == 10) {
        inventory.subparams.push(inventory.Snd.sph);
        inventory.subparams.push(inventory.Snd.cyl);
    } else if (inventory.Products.TypeId == 30) {
        inventory.subparams.push(inventory.Snd.model);
        inventory.subparams.push(inventory.Snd.color);
        if (inventory.Snd.size) {
            inventory.subparams.push(inventory.Snd.size);
        }
    } else if (inventory.Products.TypeId == 41) {
        inventory.subparams.push(inventory.Snd.model);
        inventory.subparams.push(inventory.Snd.color);
        if (inventory.Snd.size) {
            inventory.subparams.push(inventory.Snd.size);
        }
        if (inventory.Snd.function_info) {
            inventory.subparams.push(inventory.Snd.function_info);
        }
    } else if (inventory.Products.TypeId  == 40) {
        inventory.subparams.push(inventory.Snd.sph);
        inventory.subparams.push(inventory.Snd.add);
    } else if (inventory.Products.TypeId  == 20) {
        inventory.subparams.push(inventory.Snd.sph);
        inventory.subparams.push(inventory.Snd.cyl);
    }
    parent.addGoods(inventory);
}



query(goods_type);
</script>
